// 五子棋主函数

#include "server.hpp"

const std::string host = "127.0.0.1";
const std::string user = "root";
const std::string passwd = "5793148620Gjg@?+=++[]%";
const std::string dbname = "gobang";
unsigned int port = 3306;

void testlog()
{
    IFLOG("xx");
    WINGLOG("xx");
    DBGLOG("xx");
    ERRLOG("xx");
    FALOG("xx");
}

void test_util_mysql()
{

    MYSQL *mysql = mysql_util::mysql_create(host, user, passwd, dbname, port);
    const std::string sql = "insert into test_table values('我是高手','白银','1232445','2222')";
    mysql_util::SqlExecute(mysql, sql);
    mysql_util::mysql_destory(mysql);
}

void test_util_json()
{
    Json::Value root;
    std::string body;
    root["姓名"] = "张三";
    root["年龄"] = 18;
    root["成绩"].append(98);
    root["成绩"].append(78.5);
    root["成绩"].append(97.5);
    json_util::Serialize(root, body);
    DBGLOG("Serialize : %s ", body.c_str());

    Json::Value result;
    json_util::Unserialize(body, result);
    DBGLOG("姓名 : %s", result["姓名"].asString().c_str());
    DBGLOG("年龄 : %d", result["年龄"].asInt());
    int size = result["成绩"].size();
    for (int i = 0; i < size; ++i)
    {
        DBGLOG("成绩 : %.2lf", result["成绩"][i].asFloat());
    }
}

void test_util_split()
{
    std::string str = "192.168....21.155....";
    // std::string str = "192";
    DBGLOG("befor split : %s", str.c_str());
    std::vector<std::string> arry;
    std::string sep = ".";
    string_util::split(str, sep, arry);
    for (auto s : arry)
    {
        DBGLOG("%s", s.c_str());
    }
}

void test_util_file()
{
    std::string body;
    file_util::read("./Makefile", body);
    DBGLOG("%s", body.c_str());
}

void test_db()
{
    std::unique_ptr<user_table> us(new user_table(host, user, passwd, dbname, port));
    Json::Value user;
    user["username"] = "我是棋王";
    user["password"] = "123456";

    // us->Register(user);
    // 登录会返回用户详细内容, 如 : 天梯分数等 , 保存在用户的 Json::Value 对象中
    // us->Login(user);

    // us->ObtainInfoByID(1 , user);
    // us->ObtainInfoByName("我是棋王" , user);
    // us->Win(1);
    us->Lose(1);
    std::string str;
    json_util::Serialize(user, str);
    // std::cout << str << std::endl;
}

void test_online_user()
{
    std::unique_ptr<OnlineUserManage> online = std::make_unique<OnlineUserManage>();
    wsserver_t::connection_ptr test_ptr;
    online->EnterGameHall(1, test_ptr);
    online->EnterGameRoom(2, test_ptr);
    online->ExitGameHall(1);
    online->ExitGameRoom(2);
}

// 测试语法问题
void test_room()
{
    std::unique_ptr<user_table> us(new user_table(host, user, passwd, dbname, port));
    std::unique_ptr<OnlineUserManage> online = std::make_unique<OnlineUserManage>();
    std::unique_ptr<Room> room = std::make_unique<Room>(1, us.get(), online.get());
}

// 测试语法问题
void test_room_manage()
{
    std::unique_ptr<user_table> us(new user_table(host, user, passwd, dbname, port));
    std::unique_ptr<OnlineUserManage> online = std::make_unique<OnlineUserManage>();
    std::unique_ptr<RoomManage> room = std::make_unique<RoomManage>(us.get(), online.get());
}

// 测试语法问题
void test_session()
{
    wsserver_t *server;
    std::unique_ptr<SessionManage> session = std::make_unique<SessionManage>(server);
    session_status ss = LOGIN;
    session->CreateSession(1, ss);
}

// 测试语法问题
void test_match()
{
    std::unique_ptr<user_table> us(new user_table(host, user, passwd, dbname, port));
    std::unique_ptr<OnlineUserManage> online = std::make_unique<OnlineUserManage>();
    std::unique_ptr<RoomManage> room = std::make_unique<RoomManage>(us.get(), online.get());
    std::unique_ptr<MatchManage> session = std::make_unique<MatchManage>(us.get(),room.get(),online.get());
}

int main()
{
    GoBangServer server(host, user, passwd, dbname, port);
    server.Start(8089);
    
    return 0;
}